﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using TreaShares.Backoffice.Controlers;
using TreaShares.Algo;
//using TreaShares.Algo;

namespace TreaShares.Services
{
    [ServiceContract(Namespace = "TreaShares.Services")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class TransactionAdd
    {
        // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
        // To create an operation that returns XML,
        //     add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
        //     and include the following line in the operation body:
        //         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
        [OperationContract]
        public int Share(string clientId, DateTime transactionDate, DateTime valueDate, int entity, int counterparty,
            string transactionRef, char buySell, int stock, int quantity, float price, float perShare, float flat, float totalFees, float totalConsideration)
        {
            TransactionManager transactionManager = new TransactionManager();
            int transactionId = transactionManager.AddStock(new Guid(clientId), transactionDate, entity, counterparty, transactionRef, buySell, valueDate, stock, quantity, price, flat, perShare, totalFees, totalConsideration);
            return transactionId;
        }

        [OperationContract]
        public int Option(string clientId, DateTime transactionDate, DateTime valueDate, int entity, int counterparty,
            string transactionRef, char buySell, DateTime vestingDate, DateTime exerciseDate, int stock, int quantity, float strike, float premium, string exerciseType)
        {
            TransactionManager transactionManager = new TransactionManager();
            int transactionId = transactionManager.AddOption(new Guid(clientId), transactionDate, entity, counterparty, transactionRef,
                buySell, valueDate, vestingDate, exerciseDate, stock, quantity, strike, premium, exerciseType);
            return transactionId;
        }
        [OperationContract]
        [System.Web.Services.WebMethod(BufferResponse = false)]
        public string PriceOption(
             DateTime valueDate, DateTime vestingDate, DateTime maturityDate, int quantity, double stockPrice, double strike, double growthRate, double dividendRate, float volatility, float lossRate)
        {
            double euroValue = PriceEuro(valueDate, maturityDate, stockPrice, strike, growthRate, dividendRate, volatility) * quantity;
            double amerValue = PriceAmerican(valueDate, maturityDate, stockPrice, strike, growthRate, dividendRate, volatility) * quantity;
            double empValue = PriceEmp(valueDate, maturityDate, vestingDate, stockPrice, strike, growthRate, dividendRate, lossRate, volatility) * quantity;
            return string.Format("Euro:{0}, American:{1}, Emploee:{2}", Math.Round(euroValue, 4), Math.Round(amerValue, 4), Math.Round(empValue, 4));
        }

        private double PriceEuro(DateTime valueDate, DateTime maturityDate, double stockPrice, double strike, double growthRate, double dividendRate, float volatility)
        {
            VanillaPricer pricer = new VanillaPricer();
            double NPV = 0F;
            double delta = 0F;
            double gamma = 0F;
            double theta = 0F;
            pricer.PriceOptionEuro(valueDate, maturityDate, stockPrice, strike, growthRate / 100, dividendRate / 100, volatility / 100, ref NPV, ref delta, ref gamma, ref theta);
            return NPV;
        }
        private double PriceAmerican(DateTime valueDate, DateTime maturityDate, double stockPrice, double strike, double growthRate, double dividendRate, float volatility)
        {
            VanillaPricer pricer = new VanillaPricer();
            double NPV = 0F;
            double delta = 0F;
            double gamma = 0F;
            pricer.PriceOptionAmer(valueDate, maturityDate, stockPrice, strike, growthRate / 100, dividendRate / 100, volatility / 100, ref NPV, ref delta, ref gamma);
            return NPV;
        }
        private double PriceEmp(DateTime valueDate, DateTime maturityDate, DateTime vestingDate, double stockPrice, double strike, double growthRate, double dividendRate, float empLossRate, float volatility)
        {
            VanillaPricer pricer = new VanillaPricer();
            double NPV = 0F;
            double delta = 0F;
            double gamma = 0F;
            double theta = 0F;
            pricer.PriceOptionEmp(valueDate, maturityDate, vestingDate, stockPrice, strike, growthRate / 100, dividendRate / 100, empLossRate, volatility / 100, ref NPV, ref delta, ref gamma, ref theta);
            return NPV;
        }
    }
}
